package pl.edu.agh.jemo.gui.panels.objfunc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.objfunc.impl.Simple2DObjFunc;

/**
 * Configuration panel allowing to adjust simple function parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing function instance, using reference passed in constructor.
 * 
 * @author Marcin Zbijowski
 *
 */
public class SimpleConfigPanel extends JPanel {

	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 2903518488253227930L;
	/**
	 * Reference to function instance configured by panel.
	 */
	private Simple2DObjFunc function;
	/**
	 * Field allowing to set position of function extremum.
	 */
	private JTextField zeroField;

	/**
	 * Creates Panel instance associated with given function reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param objFunction reference to objective function, that needs configuration. 
	 */
	public SimpleConfigPanel(Simple2DObjFunc objFunction) {
		super();
		function = objFunction;
		
		setLayout(null);
		
		JLabel zeroLabel = new JLabel("Zero:");
		zeroLabel.setBounds(10, 10, 150, 15);
		add(zeroLabel);
		
		zeroField = new JTextField(String.valueOf(function.getZero()));
		zeroField.setBounds(10, 27, 150, 22);
		add(zeroField);
		
		zeroField.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					function.setZero(Double.parseDouble(zeroField.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");				}
			}
		});
	}

}
